package com.link4a.doms.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.link4a.doms.Constants;
import com.link4a.doms.dao.handler.MenuHandler;
import com.link4a.doms.dao.handler.RoleHandler;
import com.link4a.doms.model.Module;
import com.link4a.doms.model.Role;
import com.link4a.doms.vo.RoleCondition;
import com.link4a.doms.vo.RolePermissionInfo;
import com.link4a.doms.web.ControllerSupport;
import com.link4a.doms.web.JsonResult;
import com.link4a.doms.web.auth.Privilege;

@Controller
@RequestMapping("role")
public class RoleController extends ControllerSupport {

	@Autowired
	private RoleHandler roleHandler;
	@Autowired
	private MenuHandler menuHandler;
	
	@RequestMapping
	@ResponseBody
	public JsonResult findAll() {
		return success(roleHandler.findAll());
	}

	@RequestMapping
	@ResponseBody
	public JsonResult findPage(@RequestBody RoleCondition condition) {
		return success(roleHandler.findPage(condition));
	}
	
	@RequestMapping(value="roleAndPermission/{roleId}")
	@ResponseBody
	public JsonResult queryRoleAndPermission(@PathVariable int roleId) {
		Role role = roleHandler.findById(roleId);
		List<Module> menus = menuHandler.queryPermissionsByRoleId(roleId);
		
		RolePermissionInfo roleAndPermissionInfo = new RolePermissionInfo();
		roleAndPermissionInfo.setRoleName(role.getName());
		roleAndPermissionInfo.setDescription(role.getDescription());
		roleAndPermissionInfo.setMenus(menus);
		
		return success(roleAndPermissionInfo);
	}
	
	@RequestMapping
	@ResponseBody
	public JsonResult queryAllPermissions() {
		return success(menuHandler.queryAllPermissions());
	}
	
	@RequestMapping(method=RequestMethod.POST)
	@ResponseBody
	@Privilege(Constants.ROLE_CUD)
	public JsonResult create(@RequestBody Role role) {
		if (roleHandler.checkRoleName(role, 0)) {
			return fail("角色名已存在");
		}
		
		roleHandler.create(role);
		return  success();
	}
	
	@RequestMapping(method=RequestMethod.POST)
	@ResponseBody
	@Privilege(Constants.ROLE_CUD)
	public JsonResult update(@RequestBody Role role) {
		if (roleHandler.checkRoleName(role, 1)) {
			return fail("角色名已存在");
		}
		
		roleHandler.update(role);
		return  success();
	}
	
	@RequestMapping(value="/delete/{id}", method=RequestMethod.POST)
	@ResponseBody
	@Privilege(Constants.ROLE_CUD)
	public JsonResult delete(@PathVariable int id) {
		roleHandler.delete(id);
		return success();
	}
	
	@RequestMapping
	@Privilege(Constants.ROLE_INDEX)
	public ModelAndView index(){
		ModelAndView mav = new ModelAndView("/system/role");
		return mav;
	}
	
	@RequestMapping(value = "/add")
	@Privilege(Constants.ROLE_CUD)
	public ModelAndView add(){
		ModelAndView mav = new ModelAndView("/system/roleAdd");
		return mav;
	}
	
	@RequestMapping(value = "/edit/{roleId}")
	@Privilege(Constants.ROLE_CUD)
	public ModelAndView edit(@PathVariable int roleId){
		ModelAndView mav = new ModelAndView("/system/roleAdd");
		mav.addObject("roleId", roleId);
		return mav;
	}
}
